package com.kgc.ssm.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created On : 2022/8/11 0011.
 * <p>
 * Author : shiguangchuan
 * <p>
 * Description: 校验访问权限过滤器
 */
@WebFilter(filterName = "checkAuthFilter", urlPatterns = "/jsp/*")
public class CheckAuthFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        // 判断请求用户是否已经登录，如果登录可以直接访问管理主页，如果没有登录，直接跳转到登录
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;

        // 判断session中是否存在登录用户信息
        if(null == request.getSession().getAttribute("userCode")){
            response.sendRedirect("/login.jsp");
            return;
        }

        // 请求放行
        filterChain.doFilter(request, response);
    }


    @Override
    public void destroy() {

    }
}
